<template>
  <el-dialog title="编辑角色" :visible.sync="dialogFormVisible">
    <el-form :model="form">
      <el-form-item label="角色名称" label-width="80px">
        <el-input v-model="form.roleName" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="角色描述" label-width="80px">
        <el-input v-model="form.roleDesc" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">取 消</el-button>
      <el-button type="primary" @click.prevent="handleSubmit(form)">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { getRoleListById, editRole } from '@/api/role'
export default {
  name: 'RoleEdit',
  data () {
    return {
      dialogFormVisible: false,
      form: {}
    }
  },
  methods: {
    async showDialog (item) { // 根据角色id动态加载角色信息，显示模态框
      this.dialogFormVisible = true
      const { id } = item
      const { meta, data } = await getRoleListById(id)
      if (meta.status === 200) {
        this.form = data
        console.log(this.form)
      }
    },
    async handleSubmit (item) {
      const { roleId: id, roleName, roleDesc } = item
      const { meta } = await editRole(id, { roleName, roleDesc })
      if (meta.status === 200) {
        this.$emit('edit-success')
        this.dialogFormVisible = false
      }
    }
  }
}
</script>
<style scoped>
</style>
